package org.aaron.mp3player;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.aaron.mp3player.model.Mp3Info;
import org.aaron.mp3player.service.DownLoadService;
import org.aaron.mp3player.utils.DownloadHelper;
import org.aaron.mp3player.utils.SystemConfig;
import org.aaron.mp3player.xml.Mp3ListContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Mp3ListActivity extends ListActivity {
	// 更新菜单itemid
	private static final int UPDATE = 1;
	// 关于菜单itemid
	private static final int ABOUT = 2;

	private List<Mp3Info> mp3Infos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.remotemp3listlayout);
	}

	/**
	 * 重写optionMenu添加自定义Menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, UPDATE, 1, R.string.mp3list_update);
		menu.add(0, ABOUT, 2, R.string.mp3list_about);
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * optionMenu的按钮选择事件
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.d("Mp3ListActivity", "onOptionsItemSelected");
		Log.d("getItemId", item.getItemId() + "");
		switch (item.getItemId()) {
		case UPDATE:
			String result = downloadXml(SystemConfig.URL.WEB_SERVCE + "mp3list.xml");
			mp3Infos = parseMp3Xml(result);
			setListAdapter(bindListAdapter(mp3Infos));
			break;
		case ABOUT:
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 重写ListView Item点击事件
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Mp3Info mp3Info = mp3Infos.get(position);
		Intent intent = new Intent();
		intent.putExtra("mp3info", mp3Info);
		intent.setClass(Mp3ListActivity.this, DownLoadService.class);
		this.startService(intent);
		super.onListItemClick(l, v, position, id);
	}

	private String downloadXml(String xmlurl) {
		DownloadHelper downloadHelper = new DownloadHelper();
		String result = downloadHelper.downloadFile2String(xmlurl);
		return result;
	}

	private List<Mp3Info> parseMp3Xml(String xmlresurce) {
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		try {
			XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
			Mp3ListContentHandler contentHandler = new Mp3ListContentHandler(mp3Infos);
			xmlReader.setContentHandler(contentHandler);
			xmlReader.parse(new InputSource(new StringReader(xmlresurce)));
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (Iterator<Mp3Info> it = mp3Infos.iterator(); it.hasNext();) {
			Log.d("Mp3Info", it.next().toString());
		}
		return mp3Infos;
	}

	/**
	 * 绑定ListView适配器数据
	 * 
	 * @param mp3Infos
	 * @return
	 */
	private SimpleAdapter bindListAdapter(List<Mp3Info> mp3Infos) {
		List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
		for (Iterator<Mp3Info> iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = iterator.next();
			Map<String, String> map = new HashMap<String, String>();
			map.put("mp3_name", mp3Info.getMp3Name());
			map.put("mp3_size", mp3Info.getMp3Size());
			map.put("lrc_name", mp3Info.getLrcName());
			map.put("lrc_size", mp3Info.getLrcName());
			maps.add(map);
		}
		return new SimpleAdapter(Mp3ListActivity.this, maps, R.layout.mp3itemlayout, new String[] { "mp3_name", "mp3_size", "lrc_name", "lrc_size" }, new int[] { R.id.mp3_name, R.id.mp3_size,
				R.id.lrc_name, R.id.lrc_size });
	}
}
